package com.example.hxds.bff.driver.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.map.MapUtil;
import com.example.hxds.bff.driver.controller.form.*;
import com.example.hxds.bff.driver.service.DriverService;
import com.example.hxds.common.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;

/**
 * @ClassName: DriverController
 * @Description: 司机端BFFWeb层
 * @Author: 丛雨
 * @Date: 2024/1/13 19:32
 */

@RestController
@RequestMapping("/driver")
@Slf4j
@Tag(name = "DriverController", description = "司机Bff模块Web接口")
public class DriverController {
    @Resource
    private DriverService driverService;

    @PostMapping("/registerNewDriver")
    @Operation(summary = "新司机注册")
    public R registerNewDriver(@RequestBody @Valid RegisterNewDriverForm form) {
        long driverId = driverService.registerNewDriver(form);
        // 在SaToken上执行登录,缓存driverId,之后才可以拿到令牌
        StpUtil.login(driverId);
        // 生成Token令牌字符串
        String token = StpUtil.getTokenInfo().getTokenValue();
        return R.ok().put("token", token);
    }

    @PostMapping("/updateDriverAuth")
    @Operation(summary = "更新实名认证信息")
    @SaCheckLogin
    public R updateDriverAuth(@RequestBody @Valid UpdateDriverAuthForm form) {
        long driverId = StpUtil.getLoginIdAsLong();
        form.setDriverId(driverId);
        int rows = driverService.updateDriverAuth(form);
        return R.ok().put("rows", rows);
    }

    @PostMapping("/createDriverFaceModel")
    @Operation(summary = "创建司机人脸模型归档")
    @SaCheckLogin
    public R createDriverFaceModel(@RequestBody @Valid CreateDriverFaceModelForm form) {
        long driverId = StpUtil.getLoginIdAsLong();
        form.setDriverId(driverId);
        String result = driverService.createDriverFaceModel(form);
        return R.ok().put("result", result);
    }

    @PostMapping("/login")
    @Operation(summary = "司机登录系统")
    public R login(@RequestBody @Valid LoginForm form) {
        HashMap map = driverService.login(form);
        if (map != null) {
            Long driverId = MapUtil.getLong(map, "id");
            byte realAuth = Byte.parseByte(MapUtil.getStr(map, "realAuth"));
            boolean archive = MapUtil.getBool(map, "archive");
            // Sa-Token登录并获取Token令牌
            StpUtil.login(driverId);
            String token = StpUtil.getTokenInfo().getTokenValue();
            return R.ok().put("token", token).put("realAuth", realAuth).put("archive", archive);
        }
        return R.ok();
    }

    @GetMapping("/logout")
    @Operation(summary = "登出系统")
    @SaCheckLogin
    public R logout() {
        StpUtil.logout();
        return R.ok();
    }


    @PostMapping("/searchDriverBaseInfo")
    @Operation(summary = "查询司机基本信息")
    @SaCheckLogin
    public R searchDriverBaseInfo() {
        SearchDriverBaseInfoForm form = new SearchDriverBaseInfoForm();
        long driverId = StpUtil.getLoginIdAsLong();
        form.setDriverId(driverId);
        HashMap result = driverService.searchDriverBaseInfo(form);
        return R.ok().put("result", result);
    }

    @PostMapping("/searchWorkbenchData")
    @Operation(summary = "查询司机工作台数据")
    @SaCheckLogin
    public R searchWorkbenchData() {
        long driverId = StpUtil.getLoginIdAsLong();
        HashMap result = driverService.searchWorkbenchData(driverId);
        return R.ok().put("result", result);
    }

    @GetMapping("/searchDriverRealAuth")
    @Operation(summary = "查询司机认证状态")
    @SaCheckLogin
    public R searchDriverRealAuth() {
        SearchDriverRealAuthForm form = new SearchDriverRealAuthForm();
        long driverId = StpUtil.getLoginIdAsLong();
        form.setDriverId(driverId);
        byte realAuth = driverService.searchDriverRealAuth(form);
        return R.ok().put("realAuth", realAuth);
    }

    @GetMapping("/searchDriverAuth")
    @Operation(summary = "查询司机实名认证信息")
    @SaCheckLogin
    public R searchDriverAuth() {
        SearchDriverAuthForm form = new SearchDriverAuthForm();
        long driverId = StpUtil.getLoginIdAsLong();
        form.setDriverId(driverId);
        HashMap result = driverService.searchDriverAuth(form);
        return R.ok().put("result", result);
    }


}